package com.etv.launcher.ui.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.etv.launcher.R;
import com.etv.launcher.common.enums.DesktopItemEnum;
import com.etv.launcher.model.dto.ApplicationInfoDto;
import com.etv.launcher.model.pojo.DeskTopAdapterItem;
import com.etv.launcher.ui.actvity.desktop.DesktopFragment;
import com.etv.launcher.ui.component.ApplicationIconView;

import java.util.List;

public class DesktopAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private DesktopFragment desktopFragment;

    private final int columnCount;

    private final int viewWidth;
    private final int viewHeight;

    private List<DeskTopAdapterItem> dataList;

    private RecyclerView recyclerView;

    @SuppressLint("NotifyDataSetChanged")
    public void setDataList(List<DeskTopAdapterItem> dataList){
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    public DesktopAdapter(RecyclerView recyclerView, List<DeskTopAdapterItem> dataList, int columnCount, int viewWidth, int viewHeight, DesktopFragment desktopFragment){
        this.recyclerView = recyclerView;
        this.dataList = dataList;
        this.columnCount = columnCount;
        this.viewWidth = viewWidth;
        this.viewHeight = viewHeight;
        this.desktopFragment = desktopFragment;

        this.attachToRecyclerView();
    }

    /**
     * 设置网格格式：
     * 小组件独占一行
     * 应用图标正常排列
     */
    private void attachToRecyclerView(){
        GridLayoutManager layoutManager = new GridLayoutManager(this.recyclerView.getContext(), this.columnCount);
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return position ==0 ? columnCount: 1;
            }
        });

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(this);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == DesktopItemEnum.APPLICATIONS.getValue())
            return new ApplicationHolder(new ApplicationIconView(parent.getContext(),this.viewWidth,viewHeight),this.desktopFragment);
        else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_widget_container, parent, false);
            return new WidgetContainerHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ApplicationHolder)
            ((ApplicationHolder)holder).bind((ApplicationInfoDto) (dataList.get(position).getItem()));
    }

    @Override
    public int getItemViewType(int position) {
        return position==0? DesktopItemEnum.WIDGETS.getValue()
                : DesktopItemEnum.APPLICATIONS.getValue();
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }


    /**
     * 小组件holder
     */
    static class WidgetContainerHolder extends RecyclerView.ViewHolder {
        private final View view;

        WidgetContainerHolder(@NonNull View view) {
            super(view);
            this.view = view;
        }

    }

    /**
     * 应用列表holder
     */
    static class ApplicationHolder extends RecyclerView.ViewHolder {
        private final ApplicationIconView view;

        ApplicationHolder(@NonNull ApplicationIconView view,DesktopFragment desktopFragment) {
            super(view);
            this.view = view;
            this.view.setDesktopFragment(desktopFragment);
        }


        void bind(ApplicationInfoDto info) {
            view.bind(info);
        }
    }
}
